﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using Dou.BoundingVolumeHierarchy;
using Dou.Cluster;
using UnityEngine;

namespace Demo.WorldMap
{
    /// <summary>
    /// 使用到的预制信息
    /// </summary>
    public class PrefabInfo
    {
        public GameObject asset;
        public Bounds bounds;
    }
    
    /// <summary>
    /// 使用到的预制实例
    /// </summary>
    public class PrefabInstanceObject
    {
        public int prefabIndex;
        public Bounds bounds;
        public Vector3 position;
        public Quaternion rotation;
        public Vector3 scale;
    }

    #region 构建简单 BVH 使用

    /// <summary>
    /// BVH 构建最小节点
    /// </summary>
    public class SimpleBVHLeaf : IBVHClient
    {
        public PrefabInstanceObject instance;
        
        public Vector3 GetPosition()
        {
            return instance.bounds.center;
        }

        public Vector3 GetPreviousPosition()
        {
            return instance.bounds.center;
        }

        public Bounds GetBounds()
        {
            return instance.bounds;
        }
    }

    #endregion
    
    #region 构建蔟 BVH 使用

    /// <summary>
    /// 构建蔟使用
    /// </summary>
    public class GameObjectCluster : IKMeansClient
    {
        public GameObject gameObject;
        
        public Vector3 GetPosition()
        {
            return gameObject.transform.position;
        }
    }
    
    /// <summary>
    /// BVH 构建最小节点
    /// </summary>
    public class ClusterBVHLeaf : IBVHClient
    {
        public ClusterNode node;

        public Vector3 GetPosition()
        {
            return node.bounds.center;
        }

        public Vector3 GetPreviousPosition()
        {
            return node.bounds.center;
        }

        public Bounds GetBounds()
        {
            return node.bounds;
        }
    }
    
    /// <summary>
    /// BVH 最小节点包含的数据
    /// </summary>
    public class ClusterNode
    {
        public List<PrefabInstanceObject> instanceList;
        public Bounds bounds;
        public int subBvhIndex; // 合并 BVH 时使用
    }

    /// <summary>
    /// BVH 树转换为的自定义格式的二叉树，方便后续使用
    /// </summary>
    public class ClusterBinaryTree
    {
        public ClusterBinaryTreeNode root;
        public int nodeCount;
    }
    
    public class ClusterBinaryTreeNode
    {
        public bool isLeaf => obj != null;
        public int child1Index; // 构建时临时使用
        public int child2Index; // 构建时临时使用
        public ClusterBinaryTreeNode child1;
        public ClusterBinaryTreeNode child2;
        public Bounds aabb;
        public List<PrefabInstanceObject> obj;
        public int subBvhIndex; // 合并 BVH 时使用
    }
    
    #endregion
    
}
